package org.phoenixframework.api.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.phoenixframework.api.sys.service.ISysAuthorityService;
import org.phoenixframework.api.sys.service.ISysRoleAuthorityService;
import org.phoenixframework.api.sys.util.AuthorityUtil;
import org.phoenixframework.common.entity.SysAuthority;
import org.phoenixframework.common.entity.SysRoleAuthority;
import org.phoenixframework.common.entity.annotation.OperationLog;
import org.phoenixframework.common.tools.ResultModel;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 系统权限表 前端控制器
 * </p>
 *
 * @author doublelife
 * @since 2021-05-08
 */
@Slf4j
@Api(tags = {"系统权限"})
@AllArgsConstructor
@RestController
@RequestMapping("/sysAuthority")
public class SysAuthorityController extends BaseController {

    private final ISysAuthorityService sysAuthorityService;

    private final ISysRoleAuthorityService sysRoleAuthorityService;

    @OperationLog(elkEnable = true)
    @ApiOperation(value = "根据角色Id获取权限")
    @GetMapping("findAuthoritiesByRoleId/{roleId}")
    public ResultModel<?> findAuthoritiesByRoleId(@ApiParam(name = "roleId", value = "角色Id", required = true) @PathVariable Long roleId) {
        Map<String, Object> result = sysAuthorityService.findAuthoritiesByRoleId(roleId);
        return ResultModel.ok(result);
    }

    @OperationLog(elkEnable = true)
    @ApiOperation("为角色分配权限")
    @PostMapping("/setSysAuthority")
    public ResultModel<?> setSysAuthority(@ApiParam(name = "roleId", value = "角色Id", required = true) @RequestParam Long roleId,
                                          @ApiParam(name = "authorityIds", value = "权限Ids", required = true) @RequestParam List<Long> authorityIds) {
        // 先删除
        sysRoleAuthorityService.remove(new LambdaQueryWrapper<SysRoleAuthority>().eq(SysRoleAuthority::getRoleId, roleId));
        // 保存
        ArrayList<SysRoleAuthority> sysRoleAuthorities = new ArrayList<>();
        authorityIds.forEach(item -> {
            SysRoleAuthority sysRoleAuthority = new SysRoleAuthority();
            sysRoleAuthority.setRoleId(roleId);
            sysRoleAuthority.setAuthorityId(item);
            sysRoleAuthorities.add(sysRoleAuthority);
        });
        boolean b = sysRoleAuthorityService.saveBatch(sysRoleAuthorities);
        return this.buildByBool(b);
    }

    @OperationLog(elkEnable = true)
    @ApiOperation("获取全部数据")
    @GetMapping("/findAll")
    public ResultModel<?> findAll() {
        List<SysAuthority> list = sysAuthorityService.list(null);
        return this.buildByList(AuthorityUtil.build(list));
    }

    @OperationLog(elkEnable = true)
    @ApiOperation("保存信息")
    @PostMapping("/save")
    public ResultModel<?> save(@ApiParam(name = "sysAuthority", value = "对象信息", required = true) @Validated @RequestBody SysAuthority sysAuthority) {
        boolean result = sysAuthorityService.save(sysAuthority);
        return this.buildByBool(result);
    }

    @OperationLog(elkEnable = true)
    @ApiOperation("根据id获取一条信息")
    @GetMapping("/find/{id}")
    public ResultModel<?> findById(@ApiParam(name = "id", value = "对象信息id", required = true) @PathVariable Long id) {
        SysAuthority sysAuthority = sysAuthorityService.getById(id);
        return this.buildByObject(sysAuthority);
    }

    @OperationLog(elkEnable = true)
    @ApiOperation("根据id修改信息")
    @PutMapping("/update")
    public ResultModel<?> updateById(@ApiParam(name = "sysAuthority", value = "对象信息", required = true) @Validated @RequestBody SysAuthority sysAuthority) {
        if (StringUtils.isBlank(sysAuthority.getId().toString())) {
            return ResultModel.error();
        }
        boolean result = sysAuthorityService.updateById(sysAuthority);
        return this.buildByBool(result);
    }

    @OperationLog(elkEnable = true)
    @ApiOperation("根据id删除一条信息(递归删除子级)")
    @DeleteMapping("/remove/{id}")
    public ResultModel<?> removeById(@ApiParam(name = "id", value = "对象信息id", required = true) @PathVariable Long id) {
        boolean b = sysAuthorityService.removeChildrenById(id);
        return this.buildByBool(b);
    }

}
